import fs from "fs";
import path from "path";

export function sum(array) {
  return array.reduce((acc, cur) => acc + cur, 0);
}

export function tryFilePath(filePath) {
  const dirname = path.dirname(filePath);
  const ext = path.extname(filePath);
  const basename = path.basename(filePath, ext);
  let i = 0;
  let targetFilePath = filePath;
  while (fs.existsSync(targetFilePath)) {
    i += 1;
    targetFilePath = path.join(dirname, `${basename}(${i})${ext}`);
  }
  return targetFilePath;
}
